/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Admin.DAO;

import Admin.Model.Directory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author home
 */
public class DirectoryDAO {

    private Connection myCon;
    private PreparedStatement ps;
    private ResultSet rs;
    private String strCommand = "";

    public ArrayList<Directory> GetAll() {
        ArrayList<Directory> listDirectory = new ArrayList<Directory>();
        Directory directory;
        try {

            strCommand = "select * from tbl_Directories";
            myCon = myConnection.getConnection();
            myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            rs = ps.executeQuery();
            while (rs.next()) {
                  
               directory=new Directory();
               directory.setDirectoryID(rs.getInt("directoryID"));
               directory.setDirectoryCategoryID(rs.getInt("directoryCategoryID"));
               directory.setDirectoryName(rs.getString("directoryName"));
               directory.setDirectoryAddress(rs.getString("directoryAddress"));
               directory.setDirectoryPhone(rs.getString("directoryPhone"));
               directory.setDirectoryEmail(rs.getString("directoryEmail"));
              
               directory.setDirectoryStatus(rs.getInt("directoryStatus"));
                  
               listDirectory.add(directory);

            }
            return listDirectory;
        } catch (SQLException ex) {
            Logger.getLogger(DirectoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return listDirectory;
    }

    public boolean addDirectory(int directoryCategoryID,String directoryName,String directoryAddress,String directoryPhone,String directoryEmail) {
        boolean flag = false;

        try {
            strCommand = "INSERT INTO tbl_Directories (directoryCategoryID,directoryName ,directoryAddress ,directoryPhone ,directoryEmail,directoryStatus)"
                    + " VALUES (? ,? ,? ,? ,? ,1)";

            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, directoryCategoryID);
            ps.setString(2, directoryName);
            ps.setString(3, directoryAddress );
            ps.setString(4, directoryPhone);
            ps.setString(5, directoryEmail);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }

    public Directory GetByID(int directoryID) {
        ArrayList<Directory> listDirectory = new ArrayList<Directory>();
        Directory directory = new Directory();
        try {

            strCommand = "select * from tbl_Directories where directoryID= ?";
            myCon = myConnection.getConnection();
            myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand);
            ps.setInt(1, directoryID);
            rs = ps.executeQuery();
            while (rs.next()) {

               directory.setDirectoryID(rs.getInt("directoryID"));
               directory.setDirectoryCategoryID(rs.getInt("directoryCategoryID"));
               directory.setDirectoryName(rs.getString("directoryName"));
               directory.setDirectoryAddress(rs.getString("directoryAddress"));
               directory.setDirectoryPhone(rs.getString("directoryPhone"));
               directory.setDirectoryEmail(rs.getString("directoryEmail"));

               directory.setDirectoryStatus(rs.getInt("directoryStatus"));



            }
            return directory;
        } catch (SQLException ex) {
            Logger.getLogger(DirectoryDAO.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            myConnection.closeConnection(myCon, ps, rs);
        }
        return directory;
    }



    public boolean editDirectory(Directory objDirectory) {
        //UPDATE tbl_Directories
        boolean flag = false;
        try {
            strCommand = "UPDATE tbl_Directories SET directoryCategoryID = ?,directoryName = ?,directoryAddress = ?,directoryPhone = ?,directoryEmail = ?,directoryStatus = ? WHERE directoryID= ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, objDirectory.getDirectoryCategoryID());
            ps.setString(2, objDirectory.getDirectoryName());
            ps.setString(3, objDirectory.getDirectoryAddress());
            ps.setString(4, objDirectory.getDirectoryPhone());
            ps.setString(5, objDirectory.getDirectoryEmail());
            ps.setInt(6, objDirectory.getDirectoryStatus());
            ps.setInt(7, objDirectory.getDirectoryID());
            
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
    public boolean deleteCategory(int directoryID)
    {
         boolean flag = false;
        try {
            strCommand = "DELETE FROM tbl_Directories WHERE directoryID = ?";
            myCon = myConnection.getConnection();
            ps = myCon.prepareStatement(strCommand, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ps.setInt(1, directoryID);
            int i = ps.executeUpdate();
            if (i > 0) {
                flag = true;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            myConnection.closeConnection(myCon, ps, null);
        }
        return flag;
    }
}
